import { goodsForHome } from "../../data/goods";
Page({
  data: {
    isLoading: true,
    imgUrls: [
      'https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner1.png',
      'https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner2.png',
      'https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner3.png',
      'https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner4.png',
      'https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner5.png',
      'https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner6.png',
    ],
    tabActive: 0,
    tabs: [
      {name:'精选推荐'},
      {name:'夏日防晒'},
      {name:'二胎大作战'},
      {name:'人气榜'},
      {name:'好评榜'},
      {name:'手机也疯狂'},
    ],
    activeColor: 'red',
    goods: [],
    isLoadingOnBottom:false,
  },
  onLoad(option) {
    this.setData({goods:goodsForHome()});
    //console.log('goods:',this.data.goods);
    setTimeout(()=>{
      this.setData({isLoading:false,});
    },200);
  },
  toMore(e) {
    wx.navigateTo({
      url: '/pages/cart/more/index',
    });
  },
  toSearchPage(e) {
    wx.navigateTo({
      url: '/pages/goods/search/index',
    });
  },
  onTabChange(e) {
    /*
    wx.showToast({
      title: `切换到标签 ${e.detail.name}`,
      icon: 'none',
    });*/
    if(this.data.isLoading) return;
    this.setData({isLoading:true});
    let goods=this.data.goods.sort((a,b)=>{
      return Math.random()>0.5?1:-1;
    });
    this.setData({goods:goods});
    setTimeout(()=>{
      this.setData({isLoading:false});
    },400);
  },
  onReachBottom() {
    console.log('onReachBottom');
    if(this.data.isLoadingOnBottom) return;
    this.setData({isLoadingOnBottom:true});
    let that=this;
    setTimeout(()=>{
      let goods=that.data.goods;
      that.setData({
        goods:goods.concat(
          [goods[parseInt(Math.random()*goods.length)],
          goods[parseInt(Math.random()*goods.length)]]
        )
      });
      that.setData({isLoadingOnBottom:false});
    },1000);
  },

})
